package com.huiyujiaoyu.powertime.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.model.SimpleBean;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.AndroidBug5497Workaround;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.KeyboardUtil;
import com.huiyujiaoyu.util.StringUtils;
import com.huiyujiaoyu.util.ToastUtil;
import com.huiyujiaoyu.widget.LlkjToolBar;
import com.huiyujiaoyu.widget.SetHeightWebUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.huiyujiaoyu.http.HttpMethods.getMD5Pare;


/**
 * Created by hhwly on 2017-06-29.
 */

public class HeadingDetailsActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    @BindView(R.id.wv_content)
    WebView mWvContent;
    @BindView(R.id.ls_view)
    RecyclerView mLsView;
    @BindView(R.id.et_comment)
    EditText mEtComment;
    @BindView(R.id.tv_article_title)
    TextView mTvArticleTitle;
    @BindView(R.id.tv_read_count)
    TextView mTvReadCount;
    @BindView(R.id.ll_default)
    LinearLayout mLlDefault;
    private String wzurl;
    private List<IndexItemModel> pldatas;
    private BaseQuickAdapter<IndexItemModel, BaseViewHolder> pladapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_headling_details;
    }

    public static void launch(Context context, String content_id) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.DATA, content_id);
        ActivityUtil.startActivity(context, HeadingDetailsActivity.class, bundle);
    }

    private String content_id;

    public void initView() {
        AndroidBug5497Workaround.assistActivity(this);
        setToolbar(mToolbar);
        mToolbar.setToolBar("热门头条", true, "", R.mipmap.icon_titlebar_back, true, "", R.mipmap.icon_titlebar_share);
    }

    public void initData() {
        if (!getIntent().hasExtra(Constant.DATA)) {
            return;
        }
        content_id = getIntent().getStringExtra(Constant.DATA);
        HttpMethods.getInstance().view(mContext, getComp(), this, content_id, "0", 1);
        pldatas = new ArrayList<>();
        pladapter = new BaseQuickAdapter<IndexItemModel, BaseViewHolder>(R.layout.cell_headling_comment, pldatas) {
            @Override
            protected void convert(BaseViewHolder helper, IndexItemModel item) {
                GlideUtil.loadCircleImg((ImageView) helper.getView(R.id.cir_icon), StringUtils.getFullUrl(item.photo));
                helper.setText(R.id.tv_title, item.name)
                        .setText(R.id.tv_date, item.created_at)
                        .setText(R.id.tv_content, item.content);
            }
        };
        mLsView.setLayoutManager(new LinearLayoutManager(mContext));
        mLsView.setAdapter(pladapter);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mToolbar.setRightButtonOnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mToolbar.setRightButton2OnClickLinster(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    public void getUrl() {
        TreeMap<String, Object> map = new TreeMap<>(new Comparator<String>() {
            public int compare(String obj1, String obj2) {
                // 降序排序
                return obj1.compareTo(obj2);
            }
        });
        map.put("content_id", content_id);
        map.put("user_id", UserInfoBean.getInstance().getUid());
        map.put("type", 1);
        String timestamp = System.currentTimeMillis() + "";
        map.put("timestamp", timestamp);
        String code = getMD5Pare(map);
        wzurl = String.format(Constant.WZXQ, content_id, UserInfoBean.getInstance().getUid(), timestamp, code);
    }


    SimpleBean dataModel;
    private SetHeightWebUtil setHeightWebUtil;

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_VIEW:
                dataModel = (SimpleBean) o;
                //评论
                getUrl();
                setHeightWebUtil = new SetHeightWebUtil(mWvContent);
                setHeightWebUtil.setDoWhatListener(this);
                setHeightWebUtil.loadUrl(wzurl);
                //评论
                if (dataModel.content.comment_count > 0) {
                    HttpMethods.getInstance().cIndex(mContext, getComp(), this, content_id, dataModel.content.column_id + "", "0", 1);
                }
                column_id = dataModel.column.column_id;
                pid = dataModel.content.pid;

                mTvArticleTitle.setText(dataModel.content.title);
                mTvReadCount.setText(dataModel.content.view_count + "人阅读  " + dataModel.content.created_at);
                break;
            case HttpApi.HTTP_COMMENT_INDEX:
                pldatas.clear();
                pldatas.addAll(((SimpleListBean) o).list);
                pladapter.notifyDataSetChanged();
                break;
            case HttpApi.HTTP_COM_ADD:
                ToastUtil.makeShortText(mContext, "评论成功");
                KeyboardUtil.hideSoftKeyboard(this);
                HttpMethods.getInstance().cIndex(mContext, getComp(), this, content_id, dataModel.content.column_id + "", "0", 1);
                break;
        }
    }

    private String content;
    private int column_id, pid;

    @OnClick(R.id.btn_send)
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                content = mEtComment.getText() + "";
                if (!TextUtils.isEmpty(content)) {
                    HttpMethods.getInstance().comment(mContext, getComp(), this, content_id, content, 5, column_id, pid);
                } else {
                    ToastUtil.makeShortText(mContext, "请输入评论内容");
                }
                break;
        }
    }
}
